package com.example.websocket_chat.controller;

import com.example.websocket_chat.model.ChatMsg;

import com.example.websocket_chat.model.ResBean;
import com.example.websocket_chat.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.security.core.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author jt
 * @version 1.0.0
 * @Description 类描述
 * @createTime 2021年11月11日 15:30:00
 */
@RestController
public class UserController {


    @Autowired
    SimpMessagingTemplate simpMessagingTemplate;

    @PostMapping("/doLogin")
    public ResBean doLogin(@RequestBody User user){
        ResBean resBean = new ResBean();

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try{
            subject.login(usernamePasswordToken);


        }catch (UnknownAccountException e){
            return new ResBean(400,"账户不存在",null);
        }catch (AuthenticationException e){
            return new ResBean(400,"密码不正确",null);
        }
        Session session1 = subject.getSession();
        session1.setAttribute("user",user);
        resBean.setCode(200);
        resBean.setMsg("登录成功");
        resBean.setData(user);
        return resBean;
    }

    @GetMapping("/chat/hrs")
    public List<User> getAllUsers(@RequestParam("username") String username){
        PrincipalCollection collection = SecurityUtils.getSubject().getPrincipals();
        if (null == collection || collection.isEmpty()) {
            String userName = (String) collection.iterator().next();
            System.out.println("获取当前登录用户" + userName);
            return null;
        }
        List<User> ls = new LinkedList<>();
        if(!username.equals("张三")){
            ls.add(new User("张三","123"));
        }
        if(!username.equals("李四")){
            ls.add(new User("李四","123"));
        }
        if(!username.equals("王五")){
            ls.add(new User("王五","123"));
        }
        if(!username.equals("马六")){
            ls.add(new User("马六","123"));
        }


        return ls;
    }



    @MessageMapping("/chat")
    public void handleMsg(StompHeaderAccessor headerAccessor,ChatMsg chatMsg) {
        User user = (User) headerAccessor.getSessionAttributes().get("user");  // right
        System.out.println("--------"+user.toString());
        chatMsg.setFrom(chatMsg.getFrom());
        chatMsg.setFromNickname(chatMsg.getFrom());
        chatMsg.setDate(new Date());
        // convertAndSendToUser内部会做处理
        // 发送的最终路径是/user/用户名/queue/chat
        System.out.println("开始发送消息");
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(), "/queue/chat", chatMsg);
        System.out.println("发送消息结束");
    }

}
